package com.fujitsu.cn.m;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import sun.misc.BASE64Encoder;

public class Mail {
	private int mail_key;
	private String host = "smtp.fnst.cn.fujitsu.com";
	private String userName = "yaozhilin";
	private String password="fnst123";
	private String mail_receiver;
	private String mail_sender;
	private String subject;
	private String content;
	
	
	
	public int getMail_key()
    {
    	return mail_key;
    }
	public void setMail_key(int mail_key)
    {
    	this.mail_key = mail_key;
    }
	public String getHost()
    {
    	return host;
    }
	public void setHost(String host)
    {
    	this.host = host;
    }
	public String getUserName()
    {
    	return userName;
    }
	public void setUserName(String userName)
    {
    	this.userName = userName;
    }
	public String getPassword()
    {
    	return password;
    }
	public void setPassword(String password)
    {
    	this.password = password;
    }
	public String getMail_receiver()
    {
    	return mail_receiver;
    }
	public void setMail_receiver(String mail_receiver)
    {
    	this.mail_receiver = mail_receiver;
    }
	public String getMail_sender()
    {
    	return mail_sender;
    }
	public void setMail_sender(String mail_sender)
    {
    	this.mail_sender = mail_sender;
    }
	public String getContent()
    {
    	return content;
    }
	public void setContent(String content)
    {
    	this.content = content;
    }
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	
	
	
	public boolean sendMail(){
	  Properties properties = new Properties();	  
	  properties.put("mail.smtp.host", host);
	  properties.put("mail.smtp.auth", "true");
	  Session session = Session.getDefaultInstance(properties);
	  session.setDebug(true);
	  MimeMessage message = new MimeMessage(session);
	  try {
		message.setFrom(new InternetAddress(this.mail_sender));
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(this.mail_receiver));
		message.setSubject(subject);
		Multipart multipart = new MimeMultipart();
		BodyPart conBodyPart = new MimeBodyPart();
		
		conBodyPart.setContent("<meta http-equiv=Content-Type content=text/html; charset=utf-8>"+this.getContent(),"text/html;charset=utf-8");
		multipart.addBodyPart(conBodyPart);
		
//		BodyPart messageBodyPart = new MimeBodyPart();
//		DataSource source = new FileDataSource("");
//		messageBodyPart.setDataHandler(new DataHandler(source));
//		BASE64Encoder enc = new BASE64Encoder();
//		messageBodyPart.setFileName("=?UTF-8?" + enc.encode(affixname.getbytes()) + "?=");
//		multipart.addBodyPart(messageBodyPart);
		
		message.setContent(multipart);
		message.saveChanges();
		Transport transport = session.getTransport("smtp");
		transport.connect(host, userName, password);
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
	  
	}
 /*	public static void  main(String args[]) {
		Mail mail = new Mail();
		mail.setFrom("yaozhilin@cn.fujitsu.com");
		mail.setTo("yaozhilin@cn.fujitsu.com");
		mail.setSubject("测试");
		mail.setBody("测试<br />测试");
		mail.sendMail();
	} */
}
